package com.plian.system.service.sys.fun.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.StringUtil;
import com.plian.Tools.UuidUtil;
import com.plian.system.dto.sys.fun.SysFunDTO;
import com.plian.system.entity.sys.fun.SysFun;
import com.plian.system.mapper.sys.fun.SysFunMapper;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.node.ForestNodeMerger;
import com.plian.system.service.sys.fun.ISysFunService;
import com.plian.system.vo.sys.fun.SysFunVO;
import com.plian.system.wrapper.sys.fun.SysFunWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2024/8/1 3:29 PM
 * @Param
 * @return
 * @Version: 1.0
 **/

@Service
@AllArgsConstructor
public class SysFunServiceImpl extends ServiceImpl<SysFunMapper, SysFun> implements ISysFunService {

    private final SysFunWrapper sysFunWrapper;

    @Override
    @Transactional(rollbackFor=RuntimeException.class)
    public R<SysFun> submit(SysFunVO sysFunVO) {
        SysFun sysFun = sysFunWrapper.voToEntity(sysFunVO);
        String check = check(sysFun);
        if (StringUtil.isNotBlank(check)){
            return R.fail(check);
        }
        boolean result = saveOrUpdate(sysFun);
        if (result){
            return R.data(sysFun);
        }else {
            return R.fail();
        }
    }

    @Override
    public SysFunVO detail(SysFun sysFun) {
        SysFun one = getOne(Condition.getQueryWrapper(sysFun));
        return sysFunWrapper.entityToVO(one);
    }

    @Override
    public List<SysFunVO> tree(SysFunDTO sysFunDTO){
        String keyword = sysFunDTO.getKeyword();
        String parentId = sysFunDTO.getParentId();
        Boolean isBtn = sysFunDTO.getIsBtn();
        List<SysFunVO> sysFuns = baseMapper.list(parentId, keyword, isBtn);
        return ForestNodeMerger.merge(sysFuns);
    }

    @Override
    public R saveList(List<SysFun> sysFuns){
        for (SysFun sysFun:sysFuns){
            String check = check(sysFun);
            if (StringUtil.isNotBlank(check)){
                return R.fail(check);
            }
            if (StringUtil.isBlank(sysFun.getParentId())){
                sysFun.setParentId("0");
            }
            if (StringUtil.isBlank(sysFun.getId())){
                sysFun.setId(UuidUtil.get32UUID());
            }
        }
        boolean result = saveOrUpdateBatch(sysFuns);
        if (result){
            return R.success();
        }else {
            return R.fail();
        }
    }

    @Override
    public boolean removeBypartentId(List<String> ids) {
        List<String> allIds = new ArrayList<>();
        ids.forEach(parentId->{
            List<SysFunVO> sysFuns = baseMapper.list(parentId, "", null);
            allIds.addAll(sysFuns.stream().map(SysFunVO::getId).collect(Collectors.toList()));
        });
        allIds.addAll(ids);
        return removeByIds(allIds);
    }


    @Override
    public boolean saveOrUpdate(SysFun sysFun) {
        if (StringUtil.isBlank(sysFun.getParentId())){
            sysFun.setParentId("0");
        }
        if (StringUtil.isBlank(sysFun.getId())){
            sysFun.setId(UuidUtil.get32UUID());
        }
        boolean result = super.saveOrUpdate(sysFun);
        return result;
    }

    /**
     * @Description:  判断父子节点不能重复
     * @Author: Take-off
     * @Date: 3:46 PM 2024/8/1
     * @Param: [sysFun]
     * @return: java.lang.String
     **/
    private String check(SysFun sysFun){
        if (StringUtil.isNotBlank(sysFun.getId())){
            if (sysFun.getId().equals(sysFun.getParentId())){
                return sysFun.getName() + "父id重复!";
            }
        }
        return "";
    }

}
